import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/models/invite_user_entity.dart';
import 'package:seedfour/app/theme.dart';

import '../../../constants.dart';
import 'logic.dart';

class MineInvitePage extends StatelessWidget {
  MineInvitePage({Key? key}) : super(key: key);

  final logic = Get.find<MineInviteLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('我的邀请'),
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back_ios,
            size: 16.w,
          ),
          onPressed: () {
            Get.back();
          },
        ),
        actions: [
          IconButton(
              onPressed: () => logic.toInviteSettles(),
              icon: SvgPicture.asset(
                'assets/icons/ic_invite_record.svg',
                height: 20.w,
              )),
        ],
      ),
      body: _buildListItemBg(),
    );
  }

  List<Widget> _itemBuilder(InviteUserEntity item) {
    return [
      Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(
                child: Text(
                  item.name ?? '',
                  style: TextStyle(
                    fontSize: kFontSizeMedium,
                    fontWeight: AppFont.fontMedium,
                    color: AppColor.colorFontBlack,
                  ),
                ),
              ),
              SizedBox(height: kSpaceSmall),
              Text(
                item.mobile ?? '',
                style: TextStyle(
                  fontSize: kFontSizeSmall,
                  color: AppColor.colorFontBlack,
                ),
              ),
            ],
          ),
          SizedBox(height: kSpaceMini),
          item.referrerAt == 0
              ? const Text('转所中...')
              : Text(
                  item.endAtStr ?? '',
                  style: TextStyle(
                    fontSize: kFontSizeMini,
                    color: AppColor.colorFontGrey,
                  ),
                ),
          // : Row(
          //     children: [
          //       Text(item.startAtStr ?? '',
          //           style: TextStyle(
          //               fontSize: kFontSizeMini, color: Colors.grey)),
          //       Text('~',
          //           style: TextStyle(
          //               fontSize: kFontSizeMini, color: Colors.grey)),
          //       Text(item.endAtStr ?? '',
          //           style: TextStyle(
          //               fontSize: kFontSizeMini, color: Colors.grey)),
          //     ],
          //   ),
        ],
      ),
    ];
  }

  Widget _renderRow(BuildContext context, int index) {
    if (index < logic.inviteUsers.length) {
      return Container(
        margin: EdgeInsets.only(
            left: kSpaceSmall,
            right: kSpaceSmall,
            top: kSpaceMedium,
            bottom: index == logic.inviteUsers.length - 1 ? kSpaceMedium : 0),
        padding: EdgeInsets.all(kSpaceMedium),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(kRadiusMedium),
        ),
        child: Column(
          children: _itemBuilder(logic.inviteUsers[index]),
        ),
      );
    } else {
      return _getMoreWidget();
    }
  }

  Widget _getMoreWidget() {
    return Center(
      child: Padding(
        padding: EdgeInsets.all(kSpaceMedium),
        child: Text(
          logic.bottomTips.value,
          style: TextStyle(fontSize: kFontSizeSmall, color: Colors.grey),
        ),
      ),
    );
  }

  Widget _buildListItemBg() {
    return Obx(() {
      if (logic.isLoading.value) {
        return const Center(child: CircularProgressIndicator());
      } else {
        return ListView.builder(
          itemBuilder: _renderRow,
          itemCount: logic.inviteUsers.length + 1,
          controller: logic.scrollController,
        );
      }
    });
  }
}
